package com.procidanapps.imisteridiprocida;


import com.google.analytics.tracking.android.EasyTracker;
import com.procidanapps.imisteridiprocida.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.LauncherActivity.ListItem;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private WebView browser;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//Remove title bar
	    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		
		if(PrefsActivity.getAudioOption(this.getApplicationContext()))
		{
			Audio.playSound(this, R.raw.origovitae);
		}

		 browser = (WebView) findViewById(R.id.webView1);
		 browser.setWebViewClient(new WebViewClient() {
	            public boolean shouldOverrideUrlLoading(WebView view, String url) {
	                //Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show(); 
	                Log.v("TEST", url);
	                
                	
	                if(url.contains("default"))
	                {
	                    //Toast.makeText(getApplicationContext(), "Attiva Bottoni", Toast.LENGTH_SHORT).show();
	                    ImageButton btnUp = (ImageButton) findViewById(R.id.btn_up);
	                    btnUp.setVisibility(View.VISIBLE);
	                    ImageButton btnDown = (ImageButton) findViewById(R.id.btn_down);
	                    btnDown.setVisibility(View.VISIBLE);
	                    ImageButton btnLeft = (ImageButton) findViewById(R.id.btn_left);
	                    btnLeft.setVisibility(View.VISIBLE);
	                    ImageButton btnRight = (ImageButton) findViewById(R.id.btn_right);
	                    btnRight.setVisibility(View.VISIBLE);
	                }
	                else if(url.contains("result"))
	                {
	            		if(PrefsActivity.getAudioOption(MainActivity.this))
	            		{
		                // sonoro tromba misteri
	                	Audio.stop(MainActivity.this);
	                	Audio.playSoundSingle(MainActivity.this, R.raw.tromba);
	            		}
	                	Toast.makeText(getApplicationContext(), "Comincia la processione!", Toast.LENGTH_SHORT).show();
	                    //Toast.makeText(getApplicationContext(), "Disattiva Bottoni", Toast.LENGTH_SHORT).show();
	                    ImageButton btnUp = (ImageButton) findViewById(R.id.btn_up);
	                    btnUp.setVisibility(View.GONE);
	                    ImageButton btnDown = (ImageButton) findViewById(R.id.btn_down);
	                    btnDown.setVisibility(View.GONE);
	                    ImageButton btnLeft = (ImageButton) findViewById(R.id.btn_left);
	                    btnLeft.setVisibility(View.GONE);
	                    ImageButton btnRight = (ImageButton) findViewById(R.id.btn_right);
	                    btnRight.setVisibility(View.GONE);
	                    browser.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
	                    browser.reload();
	                }
	                else if(url.contains("index"))
	                {
	                	//ho cliccato sul bottone home da web, chiudo l'activity per tornare alla activity Login
	                	MainActivity.this.finish();
	                }
	                else{
	                    //Toast.makeText(getApplicationContext(), "Disattiva Bottoni", Toast.LENGTH_SHORT).show();
	                    ImageButton btnUp = (ImageButton) findViewById(R.id.btn_up);
	                    btnUp.setVisibility(View.GONE);
	                    ImageButton btnDown = (ImageButton) findViewById(R.id.btn_down);
	                    btnDown.setVisibility(View.GONE);
	                    ImageButton btnLeft = (ImageButton) findViewById(R.id.btn_left);
	                    btnLeft.setVisibility(View.GONE);
	                    ImageButton btnRight = (ImageButton) findViewById(R.id.btn_right);
	                    btnRight.setVisibility(View.GONE);
	                }     
                    view.loadUrl(url); 
	                return true;
	            }
	        });
		 browser.setWebChromeClient(new WebChromeClient());
		 browser.getSettings().setJavaScriptEnabled(true);
		 browser.getSettings().setLoadWithOverviewMode(true);
		 browser.getSettings().setUseWideViewPort(true);
		 browser.getSettings().setBuiltInZoomControls(false);
		 browser.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		 browser.reload();
		 
		 browser.setOnLongClickListener(new OnLongClickListener() {
			   @Override
			   public boolean onLongClick(View v) {
					// Inflate the menu; this adds items to the action bar if it is present.
				   MainActivity.this.openOptionsMenu();
					return true;
			   }
			  });
	
		 Intent intent = getIntent();
		 String username = intent.getStringExtra(LoginActivity.EXTRA_MESSAGE);
		 String url = "http://misteridiprocida.altervista.org/default.html?nome=" + username;
		 //String url = "file:///android_asset/istruzioni.html";
		 //browser.loadUrl("http://unoprocidaresidente.altervista.org/i-misteri-di-procida/index.html");
	 
		 //browser.loadDataWithBaseURL("file:///android_asset/", "default.html", "text/html", "UTF-8", "");
		 browser.loadUrl(url);
		 
		 Toast.makeText(getApplicationContext(), "Benvenuto "+username + "! Per un aiuto clicca sul tasto menu o longclick sulla mappa.", Toast.LENGTH_LONG).show();
		 
	     ImageButton btnUp = (ImageButton) findViewById(R.id.btn_up);
	   /*  btnUp.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) 
	     { WebView browser = (WebView) findViewById(R.id.webView1);
	     browser.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_UP));
	    // browser.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_DPAD_UP));
	 }});*/
	     btnUp.setOnTouchListener(new OnTouchListener() {
	    	    @Override
	    	    public boolean onTouch(View v, MotionEvent event) {
	    	        if(event.getAction() == MotionEvent.ACTION_DOWN) {
	    	        	//WebView browser = (WebView) findViewById(R.id.webView1);
	    	   	     browser.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_W));  
	    	        } else if (event.getAction() == MotionEvent.ACTION_UP) {
	    	        	//WebView browser = (WebView) findViewById(R.id.webView1);
	    	   	     browser.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_W));
	    	        }
	    	    return true;
	    	    }});
	     
	     ImageButton btnDown = (ImageButton) findViewById(R.id.btn_down);
	     btnDown.setOnTouchListener(new OnTouchListener() {
	    	    @Override
	    	    public boolean onTouch(View v, MotionEvent event) {
	    	        if(event.getAction() == MotionEvent.ACTION_DOWN) {
	    	        	//WebView browser = (WebView) findViewById(R.id.webView1);
	    	   	     browser.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_S));  
	    	        } else if (event.getAction() == MotionEvent.ACTION_UP) {
	    	        	//WebView browser = (WebView) findViewById(R.id.webView1);
	    	   	     browser.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_S));
	    	        }
	    	    return true;
	    	    }});
	     
	     ImageButton btnLeft = (ImageButton) findViewById(R.id.btn_left);
	     btnLeft.setOnTouchListener(new OnTouchListener() {
	    	    @Override
	    	    public boolean onTouch(View v, MotionEvent event) {
	    	        if(event.getAction() == MotionEvent.ACTION_DOWN) {
	    	        	//WebView browser = (WebView) findViewById(R.id.webView1);
	    	   	     browser.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_A));  
	    	        } else if (event.getAction() == MotionEvent.ACTION_UP) {
	    	        	//WebView browser = (WebView) findViewById(R.id.webView1);
	    	   	     browser.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_A));
	    	        }
	    	    return true;
	    	    }});
	     
	     ImageButton btnRight = (ImageButton) findViewById(R.id.btn_right);
	     btnRight.setOnTouchListener(new OnTouchListener() {
	    	    @Override
	    	    public boolean onTouch(View v, MotionEvent event) {
	    	        if(event.getAction() == MotionEvent.ACTION_DOWN) {
	    	        	//WebView browser = (WebView) findViewById(R.id.webView1);
	    	   	     browser.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_D));  
	    	        } else if (event.getAction() == MotionEvent.ACTION_UP) {
	    	        	//WebView browser = (WebView) findViewById(R.id.webView1);
	    	   	     browser.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_D));
	    	        }
	    	    return true;
	    	    }});
	     
	}
	
	@Override
	  public void onStart() {
	    super.onStart();

	    EasyTracker.getInstance(this).activityStart(this);  // Add this method.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();

	    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
	  }

	
	@Override
	protected void onResume() {
		super.onResume();
		if(PrefsActivity.getAudioOption(this.getApplicationContext()))
		{
			Audio.playSound(this, R.raw.origovitae);
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if(PrefsActivity.getAudioOption(this.getApplicationContext()))
		{
			Audio.stop(this);	
		}

	}
	/*
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 WebView browser = (WebView) findViewById(R.id.webView1);
	    // Check if the key event was the Back button and if there's history
	    if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) {
	    	browser.goBack();
	        return true;
	    }
	    // If it wasn't the Back key or there's no web page history, bubble up to the default
	    // system behavior (probably exit the activity)
	    return super.onKeyDown(keyCode, event);
	}
	*/
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.action_istruzioni:
	            istruzioni();
	            return true;
	        case R.id.action_legenda:
	        	legenda();
	            return true;
	        case R.id.action_exit:
	        	esci();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	public void onBackPressed (){
		browser = (WebView) findViewById(R.id.webView1);
		if(browser.getUrl().contains("default"))
		{
			
			esci();
		}
		else
		{
			MainActivity.this.finish(); 
		}
			
	
	}
	
	public void esci(){
	
	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	//(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage("Sei sicuro di voler terminare la partita?")
   .setTitle("Misteri di Procida");

// Add the buttons
builder.setPositiveButton("SI", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           // User clicked SI button
    	   MainActivity.this.finish();        	   
       }
   });
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        // User clicked NO button
 	   dialog.dismiss();
 	   //return;
    }
});
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();
	}
	
	public void istruzioni() {
		// custom dialog
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.activity_istruzioni);
		dialog.setTitle(R.string.dlg_istruzioni_titolo);
		dialog.show();
		
		Button dialogButton = (Button) dialog.findViewById(R.id.istr_btn);
		dialogButton.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {dialog.cancel();}});
		
	}
	

	
	public void legenda() {
		// custom dialog
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.activity_legenda);
		dialog.setTitle(R.string.dlg_legenda_titolo);


/*
		TextView text1 = (TextView) dialog.findViewById(R.id.text1);
		text1.setText(R.string.item_legenda_1);
		ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
		image1.setImageResource(R.drawable.abbazia);
		
		TextView text2 = (TextView) dialog.findViewById(R.id.text2);
		text2.setText(R.string.item_legenda_2);
		ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);
		image2.setImageResource(R.drawable.amici);
*/
		//Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
		// if button is clicked, close the custom dialog
		
		TextView text1 = (TextView) dialog.findViewById(R.id.text1);
		TextView text2 = (TextView) dialog.findViewById(R.id.text2);
		TextView text3 = (TextView) dialog.findViewById(R.id.text3);
		TextView text4 = (TextView) dialog.findViewById(R.id.text4);
		TextView text5 = (TextView) dialog.findViewById(R.id.text5);
		TextView text6 = (TextView) dialog.findViewById(R.id.text6);
		TextView text7 = (TextView) dialog.findViewById(R.id.text7);
		TextView text8 = (TextView) dialog.findViewById(R.id.text8);
		TextView text9 = (TextView) dialog.findViewById(R.id.text9);
		TextView text10 = (TextView) dialog.findViewById(R.id.text10);		
		TextView text11 = (TextView) dialog.findViewById(R.id.text11);
		TextView text12 = (TextView) dialog.findViewById(R.id.text12);
		TextView text13 = (TextView) dialog.findViewById(R.id.text13);
		TextView text14 = (TextView) dialog.findViewById(R.id.text14);
		TextView text15 = (TextView) dialog.findViewById(R.id.text15);
		TextView text16 = (TextView) dialog.findViewById(R.id.text16);
		TextView text17 = (TextView) dialog.findViewById(R.id.text17);
		TextView text18 = (TextView) dialog.findViewById(R.id.text18);
		TextView text19 = (TextView) dialog.findViewById(R.id.text19);
		TextView text20 = (TextView) dialog.findViewById(R.id.text20);	
		TextView text21 = (TextView) dialog.findViewById(R.id.text21);
		TextView text22 = (TextView) dialog.findViewById(R.id.text22);
		TextView text23 = (TextView) dialog.findViewById(R.id.text23);
		TextView text24 = (TextView) dialog.findViewById(R.id.text24);
		TextView text25 = (TextView) dialog.findViewById(R.id.text25);
		TextView text26 = (TextView) dialog.findViewById(R.id.text26);
		TextView text27 = (TextView) dialog.findViewById(R.id.text27);
		TextView text28 = (TextView) dialog.findViewById(R.id.text28);
		TextView text29 = (TextView) dialog.findViewById(R.id.text29);
		TextView text30 = (TextView) dialog.findViewById(R.id.text30);	
		TextView text31 = (TextView) dialog.findViewById(R.id.text31);
		TextView text32 = (TextView) dialog.findViewById(R.id.text32);
		TextView text33 = (TextView) dialog.findViewById(R.id.text33);
		TextView text34 = (TextView) dialog.findViewById(R.id.text34);
	
		text1.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_1,Toast.LENGTH_LONG).show();}});
		text2.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_2,Toast.LENGTH_LONG).show();}});
		text3.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_3,Toast.LENGTH_LONG).show();}});
		text4.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_4,Toast.LENGTH_LONG).show();}});
		text5.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_5,Toast.LENGTH_LONG).show();}});
		text6.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_6,Toast.LENGTH_LONG).show();}});
		text7.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_7,Toast.LENGTH_LONG).show();}});
		text8.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_8,Toast.LENGTH_LONG).show();}});
		text9.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_9,Toast.LENGTH_LONG).show();}});
		text10.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_10,Toast.LENGTH_LONG).show();}});
		text11.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_11,Toast.LENGTH_LONG).show();}});
		text12.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_12,Toast.LENGTH_LONG).show();}});
		text13.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_13,Toast.LENGTH_LONG).show();}});
		text14.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_14,Toast.LENGTH_LONG).show();}});
		text15.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_15,Toast.LENGTH_LONG).show();}});
		text16.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_16,Toast.LENGTH_LONG).show();}});
		text17.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_17,Toast.LENGTH_LONG).show();}});
		text18.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_18,Toast.LENGTH_LONG).show();}});
		text19.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_19,Toast.LENGTH_LONG).show();}});
		text20.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_20,Toast.LENGTH_LONG).show();}});
		text21.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_21,Toast.LENGTH_LONG).show();}});
		text22.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_22,Toast.LENGTH_LONG).show();}});
		text23.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_23,Toast.LENGTH_LONG).show();}});
		text24.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_24,Toast.LENGTH_LONG).show();}});
		text25.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_25,Toast.LENGTH_LONG).show();}});
		text26.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_26,Toast.LENGTH_LONG).show();}});
		text27.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_27,Toast.LENGTH_LONG).show();}});
		text28.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_28,Toast.LENGTH_LONG).show();}});
		text29.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_29,Toast.LENGTH_LONG).show();}});
		text30.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_30,Toast.LENGTH_LONG).show();}});
		text31.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_31,Toast.LENGTH_LONG).show();}});
		text32.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_32,Toast.LENGTH_LONG).show();}});
		text33.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_33,Toast.LENGTH_LONG).show();}});
		text34.setOnClickListener(new OnClickListener() {@Override public void onClick(View arg0) {Toast.makeText(MainActivity.this, R.string.item_legenda_descr_34,Toast.LENGTH_LONG).show();}});
	
		dialog.show();
	}

}
